package com.pyb.controller;

/**
 * @version 1.0
 * @Author 彭彦彬
 * @Date 2025/5/14 22:10
 * @注释
 */
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URL;

@WebServlet("/showImage")
public class ImageServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 图片URL
        String imageUrl = "https://gitee.com/lsls1/work-jsp/raw/master/img_2.png";

        try {
            // 获取图片的输入流
            URL url = new URL(imageUrl);
            java.awt.image.BufferedImage image = javax.imageio.ImageIO.read(url);

            // 设置响应内容类型
            response.setContentType("image/png");

            // 将图片写入到HttpServletResponse的输出流中
            javax.imageio.ImageIO.write(image, "png", response.getOutputStream());

            // 结束响应
            response.getOutputStream().flush();
        } catch (IOException e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "Failed to retrieve image.");
        }
    }
}